import {getUrlParam} from '@/static/js/filter.js';
const RongIMLib = require('@/static/js/IMSDK/RongIMLib-3.0.7-dev');
export default {
	data () {
		return {
			searchvalue: '',
			healthlist: [],
			radio: '',
			content: {},
			name: '',
			show:true,
			result:[],
			healthStr:'',
			list:[],
			checkedArr:[],
			isCheckValue: '',
			value:'',
			showModal:false,
			showContent:''
		}
	},
	created() {
		this.name = decodeURI(getUrlParam("name"));
		this.value = decodeURI(getUrlParam("name"))
		this.healthManagerId = getUrlParam("healthManagerId");
		this.appellations = getUrlParam("appellations");
		this.id = getUrlParam("id");
		this.getUserHealth();
	},
	methods: {
		getUserHealth() {
			const _this = this;
			_this.$api.UserHealth({healthRank:1,healthStr:_this.healthStr}).then((res) => {
				_this.list = res.data.result && res.data.result.list.map((a)=>{
					if(!a.headPicture){
						a.headPicture =  '../../../static/img/theHead/grzxDefault.png'
					}
					if(a.realName == _this.name){
							a.checked = true
						}else{
							a.checked = false
						}
						return a;
					});
					res.data.result && res.data.result.list.map((a)=>{
						if(_this.name.indexOf(a.realName) > -1) {
							_this.radio = a.realName;
							_this.content.headPicture = a.headPicture;
						}
					})
			})
		},
		radioChange(e) {
			e.checked = !e.checked
			this.list = this.list.map(a=>{
				if(a.id != e.id){
					a.checked = false
				}
				return a
			})
			if(!e.checked){
				this.isCheckValue = ''
				this.content.headPicture = '';
				this.healthNumber = ''
				this.appellations = ''
				this.realName = ''
				this.codeId = ''
			}else{
				this.isCheckValue = e.id
				this.healthNumber = e.healthNumber
				this.appellations = e.appellations
				this.realName = e.realName
				this.codeId = e.codeId
				this.content.headPicture = e.headPicture;
				console.log(this.realName)
			}
		},
		// 模态框confim事件
		Modalconfirm(){
			const userInfo = JSON.parse(localStorage.userInfo)
			this.$api.UserInfoupdate(userInfo.id,{healthId:this.isCheckValue,id:userInfo.uid,healthNumber:this.healthNumber,healthName:this.realName,appellations:this.appellations}).then((res)=>{
				if(res.data.returnCode == 200){
					uni.showToast({
						title: '保存成功',
						icon: 'none'
					})
					uni.navigateBack({
						delta: 1
					})
				}
			})
		},
		save() {
			const _this = this;
			if(!_this.value){
				_this.showModal = true
				_this.showContent = '暂不选择管理师将暂无管理师与你交流'
				return;
			}
			const userInfo = JSON.parse(localStorage.userInfo)
			_this.$api.UserInfoupdate(userInfo.id,{healthId:_this.isCheckValue,id:userInfo.uid,healthNumber:_this.healthNumber,healthName:_this.realName,appellations:_this.appellations}).then((res)=>{
				if(res.data.returnCode == 200){
					_this.$api.addHealth({healthId:_this.isCheckValue,healthNumber:_this.healthNumber}).then((res2)=>{
						if(res.data.returnCode == 200){
							const info = {targetId:_this.codeId,headPicture:_this.content.headPicture.substr(0,4) == 'http'?_this.content.headPicture : '',name:_this.realName}
							_this.sendmessage(info).then((message)=>{
								if(message){
									uni.navigateTo({
										url: '/pages/index/consult'
									})
								}
							})
						}
						// uni.showToast({
						// 	title: '保存成功',
						// 	icon: 'none'
						// })
					})
				}
			})
		},
		// 发送消息
		sendmessage(info){
			return new Promise((resolve,reject)=>{
				const _this = this;
				const userInfo = JSON.parse(localStorage.userInfo)
				var conversation = _this.$im.Conversation.get({
				  targetId:  info.targetId,
				  type: RongIMLib.CONVERSATION_TYPE.PRIVATE
				});
				conversation.send({
				  messageType: RongIMLib.MESSAGE_TYPE.TEXT, // 'RC:TxtMsg'
				  content: { // 填写开发者定义的消息内容
				    content: '您好。', // 文本内容
				    senduser: {
				    	id: userInfo.codeId,
				    	name: userInfo.realName,
				    	portraitUri: userInfo.avatarAddress || 'http://jlm-file.oss-cn-hangzhou.aliyuncs.com/jlm-product/image/sms/user/2020/11/5de3289f9e284f33a88d35d9b9bf8b8d.png'
				    },
				    receive:{
				    	id: info.targetId,
				    	name: info.name,
				    	portraitUri: info.headPicture || 'http://jlm-file.oss-cn-hangzhou.aliyuncs.com/jlm-product/image/sms/user/2020/11/5de3289f9e284f33a88d35d9b9bf8b8d.png'
				    },
				    senderUserId:userInfo.codeId
				  },
				  canIncludeExpansion: true, // 是否携带扩展
				  expansion: {  // 携带的扩展内容
				    isRead: false
				  }
				}).then((message) => {
				  console.log('发送携带扩展的文字消息成功', message);
				  resolve(message)
				}).catch((error) => {
				  console.log('发送携带扩展的文字消息失败', error);
				});
			})
		},
		upload() {
			vant.Dialog.confirm({
				title: '标题',
				message: '暂不选择管理师将暂无管理师与你交流',
				})
				.then(() => {
					server.post('/UserInfo/update/'+this.id+'?token='+localStorage.token,{id:this.id,healthManagerId:'',healthManagerName:''}).then((res)=>{
						if(res.data.StatusCode == 200){
							localStorage.setItem('healthId',this.content.id);
						}else{
						}
					})
				})
				.catch(() => {
					// on cancel
			});
		   
		}
	}
}